package com.qmx.launcher.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;

import com.qmx.launcher.R;
import com.qmx.launcher.callback.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

public class MyTimeActivity extends Activity {

    private static final int MSGKEY = 0x10001;

    private long exitTime = 0;

    //屏保右上角的系统时间
    private TextView mTimeTip;
    private Banner mBanner;
    //图片集合
    private List<Integer> mImages;
    //标题集合
    private List<String> titles;
    private Object selfTitle;
    private Object image;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.saver_pager);
        mBanner = (Banner) findViewById(R.id.myBanner);
        mTimeTip = (TextView) findViewById(R.id.time_tips);
        getSelfTitle();
        getImage();
        setbannerStyle();
        //启动banner
        mBanner.start();
        //        Log.e(TAG, "启动dayDream");
        mBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //启动线程刷新屏保界面右上角的时间
        new TimeThread().start();
    }

    public class TimeThread extends Thread {
        @Override
        public void run() {
            do {
                try {
                    //更新时间
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = MSGKEY;
                    mHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }

    private void setbannerStyle() {
        mBanner.setBannerStyle(BannerConfig.NOT_INDICATOR);
        //设置图片加载器
        mBanner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        mBanner.setImages(mImages);
        mBanner.setBannerAnimation(Transformer.Accordion);
        // mBanner.setBannerAnimation(Transformer.CubeOut);
        //设置标题集合
        mBanner.setBannerTitles(titles);
        //设置轮播时间
        mBanner.setDelayTime(2500);
        //设置自动播放
        mBanner.isAutoPlay(true);
        //设置指示器位置
        mBanner.setIndicatorGravity(BannerConfig.RIGHT);
    }


    public Object getSelfTitle() {
        titles = new ArrayList<String>();
        titles.add("唧唧复唧唧");
        titles.add("木兰当户织");
        titles.add("衣带渐宽终不悔");
        titles.add("为伊消得人憔悴");
        return selfTitle;
    }

    public Object getImage() {
        mImages = new ArrayList<Integer>();
        mImages.add(R.drawable.icon_gray_arrow);
        mImages.add(R.drawable.ic_launcher);
        mImages.add(R.drawable.ic_right_arrow);
        mImages.add(R.drawable.icon_close_camera);
        return image;
    }

    /**
     * 退出屏保
     * 重写方法
     *
     * @param keyCode
     * @param event
     * @return
     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        finish();
        return false;

    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSGKEY:
                    long sysTime = System.currentTimeMillis();
                    CharSequence sysTimeStr = DateFormat.format("hh:mm", sysTime);
                    mTimeTip.setText(sysTimeStr);
                    break;

            }
        }
    };
}

